<?php

/**
 * 
 */
class Model_Mapper_Reserva extends App_Model_Mapper_Abstract {

    /**
     * 
     * @var Zend_Config_Ini
     */
    protected $_config;

    /**
     * 
     * @var App_Message
     */
    protected $_message;

    /**
     * 
     * @access public
     * @return void
     */
    public function __construct() {
        $this->_config = Zend_Registry::get('config');
        $this->_message = new App_Message();
    }

    /**
     *
     * @access public
     * @param array $data
     * @return boolean
     */
    public function send(array $data) {
        try {
            $this->_data = $data;
            $this->save();
//            $mail = new Zend_Mail('utf-8');
//
//            $mail->addTo($this->_config->email->address, $this->_config->email->name);
//            $mail->setSubject('Reserva - ' . $this->_config->geral->title);
//            $mail->setBodyHtml($data['res_obs']);
//            $mail->setFrom($data['res_email'], $data['res_nome']);
//
//            $mail->send();


            $this->_message->addMessage($this->_config->messages->success, App_Message::SUCCESS);

            return true;
        } catch (Exception $e) {

            $this->_message->addMessage($this->_config->messages->error, App_Message::ERROR);

            return false;
        }
    }

    /**
     * 
     * @return App_Message
     */
    public function getMessage() {
        return $this->_message;
    }

    /**
     * 
     * @access public
     * @return void
     */
    public function save() {
        try {

            $dbReserva = App_Model_DbTable_Factory::get('Reserva');

            $adapter = $dbReserva->getAdapter();

            $adapter->beginTransaction();

            $result = parent::_simpleSave($dbReserva,false);

            $adapter->commit();

            return $result;
        } catch (Exception $e) {

            $adapter->rollBack();

            $this->_message->addMessage($this->_config->message->error, App_Message::ERROR);
        }

        return false;
    }

    /**
     * 
     * @access public
     * @return bool
     */
    public function delete() {
        try {

            $dbReserva = App_Model_DbTable_Factory::get('Reserva');

            $where = array('res_id = ?' => $this->_data['id']);

            $count = $dbReserva->delete($where);

            return !empty($count);
        } catch (Exception $e) {

            return false;
        }
    }

    /**
     * 
     * @access public
     * @return Zend_Db_Table_Rowset
     */
    public function fetchAll($filter = array()) {
        $dbReserva = App_Model_DbTable_Factory::get('Reserva');

        $select = $dbReserva->select()->setIntegrityCheck(false);
        
        if (!empty($filter['res_id'])) {
            $select->where("r.res_id = ? ", $filter['res_id']);
        }
        if (!empty($filter['res_nome'])) {
            $select->where("l.res_nome = ? ", $filter['res_nome']);
        }
        $rows = $dbReserva->fetchAll($select);

        return $rows;
    }

    /**
     * 
     * @access public
     * @return Zend_Db_Table_Row
     */
    public function fetchRow() {
        $dbReserva = App_Model_DbTable_Factory::get('Reserva');

        $where = array('res_id = ?' => $this->_data['id']);

        return $dbReserva->fetchRow($where);
    }

}
